#!/bin/bash
#脚本运行命令： ./etcd.sh node 192.168.1.206
ETCD_NAME=${1:-"master"}
ETCD_IP=${2:-"https://192.168.1.206"}

cat <<EOF >/usr/local/kubernetes/cfg/etcd
# [member]
ETCD_NAME=${ETCD_NAME}
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"  
ETCD_LISTEN_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_LISTEN_CLIENT_URLS="https://${ETCD_IP}:2379"
#[cluster]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://${ETCD_IP}:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="https://${ETCD_IP}:2379"
EOF

cat <<EOF >/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
Documentation=https://github.com/coreos
[Service]
Type=notify
EnvironmentFile=-/usr/local/kubernetes/cfg/etcd  
ExecStart=/usr/local/kubernetes/bin/etcd \
  --name ${ETCD_NAME} \
  --cert-file=/usr/local/kubernetes/ssl/etcd.pem \
  --key-file=/usr/local/kubernetes/ssl/etcd-key.pem \
  --peer-cert-file=/usr/local/kubernetes/ssl/etcd.pem \
  --peer-key-file=/usr/local/kubernetes/ssl/etcd-key.pem \
  --trusted-ca-file=/usr/local/kubernetes/ssl/ca.pem \
  --peer-trusted-ca-file=/usr/local/kubernetes/ssl/ca.pem \
  --initial-advertise-peer-urls ${ETCD_INITIAL_ADVERTISE_PEER_URLS} \
  --listen-peer-urls ${ETCD_LISTEN_PEER_URLS} \
  --listen-client-urls ${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 \
  --advertise-client-urls ${ETCD_ADVERTISE_CLIENT_URLS} \
  --initial-cluster-token ${ETCD_INITIAL_CLUSTER_TOKEN} \
  --initial-cluster master=https://192.168.1.208:2380,node1=https://192.168.1.206:2380,node2=https://192.168.1.207:2380 \
  --initial-cluster-state new \
  --data-dir=${ETCD_DATA_DIR}
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
